package com.espirit.finance.core;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.espirit.EntityMapping;
import com.espirit.eap.ICheckModelDuplicateInfo;
import com.espirit.eap.annotation.Dict;
import com.espirit.eap.annotation.DomainModel;
import com.espirit.eap.annotation.ModelDisplayProperty;
import com.googlecode.cswish.annotation.QLFormula;
import com.googlecode.cswish.model.Saas;

@Entity
@DomainModel
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@ModelDisplayProperty("name")
@BatchSize(size=15)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class AcctItem extends EntityMapping implements Serializable, Flag, ICheckModelDuplicateInfo, Saas{
	private static final long serialVersionUID = -1598913579126854546L;
	public static final Integer FIXED = 1;
	
	public static final String CALC_STRATEGY_STYLE = "acctitem_calc_strategy_style";
	public static final Integer STRATEGY_STYLE_CASCADE = 1;
	public static final Integer STRATEGY_STYLE_CASCADE_SUM = 2;
	public static final Integer STRATEGY_STYLE_PROPERTY = 3;
	
	private Integer sid;
	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}

	public BigDecimal getFixedAmount() {
		return fixedAmount;
	}
	public void setFixedAmount(BigDecimal fixedAmount) {
		this.fixedAmount = fixedAmount;
	}

	@Dict(type=CALC_STRATEGY_STYLE)
	public Integer getStyle() {
		return style;
	}
	public void setStyle(Integer style) {
		this.style = style;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public AcctItemRef getAcctItemRef() {
		return acctItemRef;
	}
	public void setAcctItemRef(AcctItemRef acctItemRef) {
		this.acctItemRef = acctItemRef;
	}

	@OneToMany(targetEntity = AcctItemScope.class ,orphanRemoval = true, fetch=FetchType.LAZY,  cascade = CascadeType.ALL, mappedBy = "parent")
	public List<AcctItemScope> getAcctItemScopes() {
		return acctItemScopes;
	}
	public void setAcctItemScopes(List<AcctItemScope> acctItemScopes) {
		this.acctItemScopes = acctItemScopes;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public Acct getParent() {
		return parent;
	}
	public void setParent(Acct parent) {
		this.parent = parent;
	}
	
	@Dict(type=ACTIVE)
	public Integer getActive() {
		return active;
	}

	public void setActive(Integer active) {
		this.active = active;
	}
	

	@Transient
	@QLFormula(value = "active", type = Integer.class)
	public String getActiveFlag() {
		if (this.active == null ){
			return ACTIVE_NO.toString();
		} 
		return this.active.toString();
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	public AcctItemFactor getAcctItemFactor() {
		return acctItemFactor;
	}
	public void setAcctItemFactor(AcctItemFactor acctItemFactor) {
		this.acctItemFactor = acctItemFactor;
	}
	
	@Column(length=2000)
	public String getTriggerCondition() {
		return triggerCondition;
	}
	public void setTriggerCondition(String condition) {
		this.triggerCondition = condition;
	}
	
	private String name ;
	private String remark;
	private Acct parent;
	private BigDecimal fixedAmount;
	private Integer style;
	private AcctItemRef acctItemRef;
	private List<AcctItemScope> acctItemScopes;
	private Integer active;
	private AcctItemFactor acctItemFactor;
	private String triggerCondition;
	private String conditionAmount;
	
	private final static String[] gatherProperties = new String[]{"name"};
	@Override
	public String[] gatherProperties() {
		return gatherProperties;
	}
	
	
	@Column(length = 2000)
	public String getConditionAmount() {
		return conditionAmount;
	}
	public void setConditionAmount(String conditionAmount) {
		this.conditionAmount = conditionAmount;
	}
}
